package chan.project.smartlaweveryman.strategy.register;

import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.exception.BusinessException;
import chan.project.smartlaweveryman.mapper.UserInfoMapper;
import chan.project.smartlawmodel.dto.lawyer.LawyerInfoRegisterRequest;
import chan.project.smartlawmodel.dto.user.UserInfoRegisterRequest;
import chan.project.smartlawmodel.entity.LawyerInfo;
import chan.project.smartlawmodel.entity.UserInfo;
import chan.project.smartlawservice.commonService.CommonInfoFeignClient;
import chan.project.smartlawservice.lawyerService.LawyerInfoFeignClient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class LawyerRegisterStrategy extends CommonRegisterStrategy{

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private LawyerInfoFeignClient lawyerInfoFeignClient;
    @Override
    public Long saveUser(UserInfoRegisterRequest userInfoRegisterRequest) {
        LawyerInfoRegisterRequest lawyerInfoRegisterRequest = (LawyerInfoRegisterRequest) userInfoRegisterRequest;
        LawyerInfo lawyerInfo = new LawyerInfo();
        BeanUtils.copyProperties(lawyerInfoRegisterRequest,lawyerInfo);
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.eq(UserInfo::getUserAccount,lawyerInfoRegisterRequest.getUserAccount());
        UserInfo userInfo = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
        lawyerInfo.setUserId(userInfo.getId());
        boolean save = lawyerInfoFeignClient.lawyerRegister(lawyerInfo);
        if(!save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"律师用户注册失败");
        }
        return userInfo.getId();
    }
}
